/**************************************************************
 *@ProjectName: blweibo
 *@FileName: SettingsPref.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.io.File;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.widget.Toast;

import com.zhiweibo.models.StaticInfo;
import com.zhiweibo.util.Helper;

/**
 * The Class SettingsPref.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class SettingsPref extends PreferenceActivity implements OnPreferenceClickListener, OnMultiChoiceClickListener,
        OnClickListener, OnPreferenceChangeListener {

    /** The defaultpreference. */
    private static SharedPreferences defaultpreference;

    /** The category reming dialog. */
    private AlertDialog categoryRemingDialog;

    /** The clear cache dialog. */
    private AlertDialog clearCacheDialog;

    /**
     * Checks if is auto load more.
     * 
     * @param paramContext
     *            the param context
     * @return true, if is auto load more
     */
    static boolean isAutoLoadMore(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("autoload_more", false);
    }

    /**
     * Gets the interval.
     * 
     * @param paramContext
     *            the param context
     * @return the interval
     */
    static int getInterval(Context paramContext) {
        return Integer.parseInt(getDefaultSharedPreference(paramContext).getString("interval", "30000"));
    }

    /**
     * Gets the text size.
     * 
     * @param paramContext
     *            the param context
     * @return the text size
     */
    static int getTextSize(Context paramContext) {
        return Integer.parseInt(getDefaultSharedPreference(paramContext).getString("textsize", "16"));
    }

    /**
     * Checks if is auto remind.
     * 
     * @param paramContext
     *            the param context
     * @return true, if is auto remind
     */
    static boolean isAutoRemind(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("auto_remind", true);
    }

    /**
     * Checks if is audio on.<br>
     * 
     * @param paramContext
     *            the param context
     * @return true, if is audio on
     */
    static boolean isAudioOn(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("audio", true);
    }

    /**
     * Checks if is notify at.
     * 
     * @param paramContext
     *            the param context
     * @return true, if is notify at
     */
    static boolean isNotifyAt(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("notify_at", true);
    }

    /**
     * Checks if is notify comment.
     * 
     * @param paramContext
     *            the param context
     * @return true, if is notify comment
     */
    static boolean isNotifyComment(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("notify_comment", true);
    }

    /**
     * Checks if is notify fans.
     * 
     * @param paramContext
     *            the param context
     * @return true, if is notify fans
     */
    static boolean isNotifyFans(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("notify_fans", true);
    }

    /**
     * Checks if is notify message.
     * 
     * @param paramContext
     *            the param context
     * @return true, if is notify message
     */
    static boolean isNotifyMessage(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("notify_message", true);
    }

    /**
     * Checks if is notify new weibo.<br>
     * always to remind the new weibo
     * 
     * @param paramContext
     *            the param context
     * @return true, if is notify new weibo
     */
    static boolean isNotifyNewWeibo(Context paramContext) {
        // return
        // getDefaultSharedPreference(paramContext).getBoolean("notify_newblog",
        // false);
        return true;
    }

    /**
     * Checks if is vibrator on.<br>
     * 
     * @param paramContext
     *            the param context
     * @return true, if is vibrator on
     */
    static boolean isVibratorOn(Context paramContext) {
        return getDefaultSharedPreference(paramContext).getBoolean("vibrator", true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting);
        PreferenceScreen localPreferenceScreen = getPreferenceScreen();
        localPreferenceScreen.findPreference("remind_category").setOnPreferenceClickListener(this);
        localPreferenceScreen.findPreference("clear_cache").setOnPreferenceClickListener(this);
        localPreferenceScreen.findPreference("auto_remind").setOnPreferenceChangeListener(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int paramInt) {
        Dialog localObject = null;
        switch (paramInt) {
        case 1000:
            localObject = new AlertDialog.Builder(this).setTitle(R.string.setting_title)
                    .setMessage(R.string.setting_need_clear_cache).setPositiveButton(R.string.ok, this)
                    .setNegativeButton(R.string.cancel, null).create();
            clearCacheDialog = ((AlertDialog) localObject);
            break;
        case 1001:
            localObject = new ProgressDialog(this);
            ProgressDialog localProgressDialog = (ProgressDialog) localObject;
            String str = getString(R.string.setting_doing);
            localProgressDialog.setMessage(str);
            break;
        case 1002:
            AlertDialog.Builder localBuilder = new AlertDialog.Builder(this).setTitle(R.string.guest_label);
            String[] arrayOfString = getResources().getStringArray(R.array.category_reming_arrsy);
            boolean[] arrayOfBoolean = readCategorySetting();
            localObject = localBuilder.setMultiChoiceItems(arrayOfString, arrayOfBoolean, this)
                    .setNegativeButton(R.string.close, null).create();
            categoryRemingDialog = ((AlertDialog) localObject);
            break;
        }
        return localObject;
    }

    /**
     * Gets the default shared preference.
     * 
     * @param paramContext
     *            the param context
     * @return the default shared preference
     */
    static SharedPreferences getDefaultSharedPreference(Context paramContext) {
        if (defaultpreference == null) {
            defaultpreference = PreferenceManager.getDefaultSharedPreferences(paramContext);
        }
        return defaultpreference;
    }

    /**
     * Read category setting.
     * 
     * @return the boolean[]
     */
    private boolean[] readCategorySetting() {
        SharedPreferences localSharedPreferences = getDefaultSharedPreference(this);
        boolean[] arrayOfBoolean = new boolean[5];
        // boolean bool1 = localSharedPreferences.getBoolean("notify_newblog",
        // false);
        boolean bool1 = localSharedPreferences.getBoolean("notify_newblog", true);
        arrayOfBoolean[0] = bool1;
        boolean bool2 = localSharedPreferences.getBoolean("notify_at", true);
        arrayOfBoolean[1] = bool2;
        boolean bool3 = localSharedPreferences.getBoolean("notify_comment", true);
        arrayOfBoolean[2] = bool3;
        boolean bool4 = localSharedPreferences.getBoolean("notify_message", true);
        arrayOfBoolean[3] = bool4;
        boolean bool5 = localSharedPreferences.getBoolean("notify_fans", true);
        arrayOfBoolean[4] = bool5;
        return arrayOfBoolean;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.preference.Preference.OnPreferenceClickListener#onPreferenceClick
     * (android.preference.Preference)
     */
    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference.getKey().equals("remind_category")) {
            showDialog(1002);
        } else if (preference.getKey().equals("clear_cache")) {
            showDialog(1000);
        }
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.content.DialogInterface.OnMultiChoiceClickListener#onClick(android
     * .content.DialogInterface, int, boolean)
     */
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        SharedPreferences.Editor localEditor;
        if (dialog == categoryRemingDialog) {
            localEditor = getDefaultSharedPreference(this).edit();
            switch (which) {
            case 0:
                localEditor.putBoolean("notify_newblog", isChecked);
                break;
            case 1:
                localEditor.putBoolean("notify_at", isChecked);
                break;
            case 2:
                localEditor.putBoolean("notify_comment", isChecked);
                break;
            case 3:
                localEditor.putBoolean("notify_message", isChecked);
                break;
            case 4:
                localEditor.putBoolean("notify_fans", isChecked);
                break;
            }
            localEditor.commit();
            if (isCancelAllCategorySetting()) {
                stopService(new Intent(SettingsPref.this, WeiboEventService.class));
            } else {
                startService(new Intent(SettingsPref.this, WeiboEventService.class));
            }
        }
    }

    /**
     * Checks if is cancel all category setting.
     * 
     * @return true, if is cancel all category setting
     */
    private boolean isCancelAllCategorySetting() {
        SharedPreferences localSharedPreferences = getDefaultSharedPreference(this);
        boolean bool1 = localSharedPreferences.getBoolean("notify_newblog", true);
        // boolean bool1 = localSharedPreferences.getBoolean("notify_newblog",
        // false);
        boolean bool2 = localSharedPreferences.getBoolean("notify_at", true);
        boolean bool3 = localSharedPreferences.getBoolean("notify_comment", true);
        boolean bool4 = localSharedPreferences.getBoolean("notify_message", true);
        boolean bool5 = localSharedPreferences.getBoolean("notify_fans", true);
        return !(bool1 || bool2 || bool3 || bool4 || bool5);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.content.DialogInterface.OnClickListener#onClick(android.content
     * .DialogInterface, int)
     */
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if ((dialog == clearCacheDialog) && (which == -1)) {
            (new AsyncTask() {
                @Override
                protected Object doInBackground(Object... paramArrayOfVoid) {
                    WeiboDB wd = WeiboDB.getInstance(SettingsPref.this);
                    wd.deleteAllAt();
                    wd.deleteAllComment();
                    wd.deleteAllMessage();
                    wd.deleteAllTopic();
                    wd.deleteUser();
                    StaticInfo.cleanDate();
                    Helper.delString(SettingsPref.this, "newId");
                    StaticInfo.map.clear();
                    File localFile1 = Environment.getExternalStorageDirectory();
                    File path = new File(localFile1.getPath() + File.separator + "zhiweibo/weibo");
                    for (File f : path.listFiles()) {
                        f.delete();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Object paramVoid) {
                    super.onPostExecute(paramVoid);
                    dismissDialog(1001);
                    Toast.makeText(SettingsPref.this, R.string.setting_success_clear, Toast.LENGTH_SHORT).show();
                }

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    showDialog(1001);
                }
            }).execute(0);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange
     * (android.preference.Preference, java.lang.Object)
     */
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference.getKey().equals("auto_remind")) {
            if (!(Boolean) newValue) {
                stopService(new Intent(SettingsPref.this, WeiboEventService.class));
            } else {
                startService(new Intent(SettingsPref.this, WeiboEventService.class));
            }
        }
        return false;
    }
}
